<?php 
/**
 * 订单表模型
 */
class OrdersModel extends Model{
	
	public $table = 'orders';
	
	//验证地址为空
	public function validateAdd(){
		if(Q('post.consignee')=='' || Q('post.address')=='' || Q('post.mobile')==''){
			$this->error = '收货地址信息填写有误，订单提交失败,请重新提交订单';
			return false;
		}
		return true;
	} 

	
	public function addOrders(){
		if(!$this->validateAdd()) return false;		
		$data = array(
			'onumber'	   => cart::getOrderId(),
			'consignee'    => Q('post.consignee'),
			'address' 	   => Q('post.address'),
			'mobile' 	   => Q('post.mobile'),
			'totalprice'   => $_SESSION['cart']['totalPrice'],
			'remarks'	   => Q('post.remarks'),
			'generatetime' => time(),
			'state'		   => 1,
			'user_uid'	   => $_SESSION['uid']
		);
		$ordersId = $this->add($data);
		//添加订单列表
		//获取从购物车session中订单数据
		$orderData = array();
		foreach ($_SESSION['cart']['confirmSid']['sid'] as $v) {
			$orderData[$v] = $_SESSION['cart']['goods'][$v];
		}

		//循环插入数据
		foreach ($orderData as $k => $v) {
			//整理订单的规格
			$spec = array();
			foreach ($v['options'] as $key => $value) {
				$spec[] = $key . ':' . $value;
			}
			$listData = array(
				'goods_gid'  => $v['id'],
				'orders_oid' => $ordersId,
				'buynum'	 => $v['num'],
				'price'	 	 => $v['total'],
				'pic'		 => $v['smallImg'],
				'ogname'	 => $v['name'],
				'spec'		 => implode(',', $spec)
			);
			K('OrdersList')->add($listData);
			//清除购买的购物车商品
			unset($_SESSION['cart']['goods'][$k]);

		}

		return true;
		
		
		
	}
	
	
	
	
}






















 ?>